home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Networking / Network Watch (DMZ) v1.5 / sources / SpinCursor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-25  |  3.9 KB  |  130 lines  |  [TEXT/MMCC]

  1. /*
  2.     File: SpinCursor.c
  3.     By: Nitin Ganatra.
  4.     Modifications by Rich Kubota
  5. */
  6.  
  7. #include "SpinCursor.h"        /* Get the prototypes for the application.        */
  8.  
  9. /* Some module-local globals */
  10. static short     gTickInterval;            // number of ticks between a frame switch */
  11. static long        gTickDefer;                // time to wait before spinning the cursor
  12. static long     gLastTick;                // tick count of last call to SpinCursor */
  13. static animatedCursorHandle gFrameList;    // our cursor list */
  14.  
  15. //----------------------------------------------------------------------------
  16. // InitAnimatedCursors
  17. //
  18. // Init the cursor information from the current resource file
  19. // acurID: 'acur' resource ID to use for the busy cursor
  20. // 
  21. //----------------------------------------------------------------------------
  22.  
  23. Boolean InitAnimatedCursors(short acurID)
  24. {
  25.     register short i=0;
  26.     register short cursID;
  27.     Boolean noErrFlag = false;
  28.     
  29.     gFrameList = (animatedCursorHandle)GetResource('acur',acurID);
  30.     if (gFrameList != NULL) {
  31.  
  32.         /* got it! */
  33.         noErrFlag = true;
  34.         while((i<(*gFrameList)->numberOfFrames) && noErrFlag)  {
  35.  
  36.             /* The id of the cursor is stored in the high word of the frame handle */
  37.             cursID = (short) HiWrd((long) (*gFrameList)->frame[i]);
  38.             (*gFrameList)->frame[i] = GetCursor(cursID);
  39.             if((*gFrameList)->frame[i])
  40.                 i++;                            /* get the next one */
  41.             else
  42.                 noErrFlag=false;    /* foo! we couldn't find the cursor */
  43.         }
  44.     }
  45.         // set cursor to no spin
  46.     gTickDefer = kDontSpinCursor;
  47.     return noErrFlag;
  48. }
  49.  
  50. //----------------------------------------------------------------------------
  51. // StartAnimatedCursors - inits the interval and deferral period for spinning the cursor
  52. //
  53. // interval: the interval that must pass from spinning the cursor last, which
  54. //            must pass before spinning the cursor again. 
  55. // deferral: number of ticks after the InitAnimatedCursors call is made that
  56. //            spinning will occur.  If the gTickDefer is -1
  57. //            then the cursor will never spin
  58. //
  59. //----------------------------------------------------------------------------
  60. void StartAnimatedCursors(short interval, short deferral)
  61. {
  62.     /* We have the cursors, now initialize the other fields */
  63.     gTickInterval = interval;
  64.     gLastTick = TickCount();
  65.     gTickDefer = deferral + gLastTick;
  66.     (*gFrameList)->whichFrame = 0;
  67. }
  68.  
  69. void StopAnimatedCursors(void)
  70. {
  71.     gTickDefer = kDontSpinCursor;
  72. }
  73.  
  74. //----------------------------------------------------------------------------
  75. // ReleaseAnimatedCursors
  76. //----------------------------------------------------------------------------
  77. void ReleaseAnimatedCursors(void)
  78. {
  79.     short i;
  80.     
  81.     gTickDefer = kDontSpinCursor;    // make sure that we can't spin the cursor
  82.     for(i = 0; i < (*gFrameList)->numberOfFrames; i++)
  83.         ReleaseResource((Handle) (*gFrameList)->frame[i]);
  84.     ReleaseResource((Handle) gFrameList);
  85.  
  86. }
  87.  
  88.  
  89. //----------------------------------------------------------------------------
  90. // SpinTheCursor
  91. //
  92. // Just call this whenever you need to increment the spinning cursor.
  93. // A typical sequence would look like this:
  94. //
  95. //  InitAnimatedCursor(1002);
  96. //    StartAnimatedCursors(6, 30);
  97. //  for (x = 1; x < kMaxSomething; x ++) {
  98. //    SpinTheCursor();
  99. //    DoTimeConsumingThing();
  100. //  }
  101. // StopAnimatedCursors();
  102. // ReleaseAnimatedCursors();
  103. //----------------------------------------------------------------------------
  104. void SpinTheCursor(void)
  105. {
  106.     register long newTick;
  107.     
  108.         // check gTickInterval
  109.     if (gTickDefer == kDontSpinCursor)
  110.         return;
  111.     
  112.         // we're spinning, so check the current time
  113.     newTick = TickCount();
  114.     
  115.         // have we passed the deferral period
  116.     if (newTick < (unsigned long)gTickDefer)
  117.         return;        // nope
  118.         
  119.     /* Is it time? */
  120.     if(newTick < (gLastTick + gTickInterval))
  121.         return;        /* nope */
  122.     
  123.     /* Grab the frame, increment (and reset, if necessary) the count, and
  124.        display the new cursor */
  125.     SetCursor(*((*gFrameList)->frame[(*gFrameList)->whichFrame++]));
  126.     if((*gFrameList)->whichFrame == (*gFrameList)->numberOfFrames)
  127.         (*gFrameList)->whichFrame = 0;
  128.     gLastTick = newTick;
  129. }
  130.